<% include common/header.ejs %>
<% include common/index.ejs %>

<div class="layui-main">
    <h1><%- data.title %></h1>

    <div class="layui-text layui-row">
        <div class="layui-col-mdl">发布人: <%- data.author.username %></div>
        <div>发布时间: <%- new Date(data.time).toLocaleString() %></div>
    </div>
    <fieldset class="layui-elem-field layui-field-title">
        <legend>任务详情</legend>
    </fieldset>
    <div>
        <%- data.content %>
    </div>
    <div>
        已接取人数：<%- data.receiver.length  %>人
        最高接取人数：<% data.num %>人
        任务时限：<%- data.expiration  %>之前
        任务难度：<div id="expiration"></div>
    </div>
    <div>
        <% if(data.can || !(people === -1)){ %>
            <button class="layui-btn layui-btn-normal layui-btn-disabled">不能接取</button>
        <% }else{ %>
                <button id="oncereceiver" class="layui-btn layui-btn-normal ">立即接取</button>
        <% } %>
    </div>
    <fieldset class="layui-elem-field layui-field-title">
        <legend>接取人</legend>
    </fieldset>
    <div>
        <% for(var i = 0; i < data.receiver.length; i++){ %>
        <%- `${i + 1}. ${data.receiver[i].user.username}` %>
        <% } %>
    </div>
    <fieldset class="layui-elem-field layui-field-title">
        <legend>完成详情</legend>
    </fieldset>
    <% for(var i = 0;i < data.receiver.length; i++){ %>
        <div class="layui-card">
            <div class="layui-card-header">用户名:<%- data.receiver[i].user.username %></div>
            <% if(data.receiver[i].finmsg){ %>
                <div class="layui-card-body">评论:<%- data.receiver[i].msg %></div>
            <% }else{ %>
                <% if(user && user._id == data.receiver[i].user._id){ %>
                    <form class="layui-form " action="">
                        <input name="id" type="text" hidden value="<%- data.receiver[i].user._id %>" />
                        <input type="text" name="index" hidden value="<%- i %>">
                        <textarea name="con" id="demo<%- i %>" style="display: none;"></textarea>
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>                    </form>

                <% } %>
            <% } %>
        </div>
    <% } %>
</div>
<script>
    layui.use(['rate','layedit','form'], () => {
        var rate = layui.rate;
        var layedit = layui.layedit;
        var form = layui.form;
        var edit = {};

        <% for(var i = 0; i < data.receiver.length; i++){ %>

       edit.index<%- i %> = layedit.build('demo<%- i %>'); //建立编辑器
        <% } %>

        //监听提交
        form.on('submit(formDemo)', function(data){
            var da = data.field;
            da.con = layedit.getContent(edit['index' +data.field.index]);
            da.taskid = location.pathname;
            layui.$.ajax({
                url:'/task/finmsg',
                method:'post',
                data:da
            })
            return false;
        });
        rate.render({
            elem:'#expiration',
            value:<%- data.difficulty %>,
            readonly:true
        })
        //console.log(location.pathname);
        layui.$('#oncereceiver').on('click', () => {
            //console.log(1);
            layui.$.ajax({
                    url:location.pathname,
                    method:'post'
                })


        })
    })
</script>
</body>
</html>